import base64
import json
import random
import time
import  requests
import config

#地点位置接口
location_url="https://apis.map.qq.com/ws/geocoder/v1/"

qq_url="https://qmsg.zendee.cn/send/"
tg_url="https://dianbao.vercel.app/send/"
now = time.gmtime(time.time()+8 * 60 * 60 * 1000)

dateCode = [
    "s9ZS", "jQkB", "RuQM", "O0_L", "Buxf", "LepV", "Ec6w", "zPLD", "eZry",
    "QjBF", "XPB0", "zlTr", "YDr2", "Mfdu", "HSoi", "frhT", "GOdB", "AEN0",
    "zX0T", "wJg1", "fCmn", "SM3z", "2U5I", "LI3u", "3rAY", "aoa4", "Jf9u",
    "M69T", "XCea", "63gc", "6_Kf"
]
hourCode = [
    "89KC", "pzTS", "wgte", "29_3", "GpdG", "FDYl", "vsE9", "SPJk", "_buC",
    "GPHN", "OKax", "_Kk4", "hYxa", "1BC5", "oBk_", "JgUW", "0CPR", "jlEh",
    "gBGg", "frS6", "4ads", "Iwfk", "TCgR", "wbjP"
]

# base64编码
def tobase64(rawstr="默认值"):
    # 编码
    encode_str = base64.encodebytes(json.dumps(rawstr).encode('utf8')).decode()  # b'aGVsbG8gd29ybGQh\n' 默认以utf8解码，结果 aGVsbG8gd29ybGQh
    #print("编码后的字符串"+encode_str)  #
    return encode_str

#获得时间戳
def gettimestamp():
    timestamp=int(time.time()) #秒数时间戳
    return timestamp

def mrdk_key(d, h):
    return dateCode[d] + hourCode[h]

#经纬度获取
def get_location_info():
    playload = {
        "address":config.myinfo["xxdz"],
        #  请勿滥用，不然到时候谁也没得用
        "key": "PULBZ-BSEWU-MAEVV-2IAJR-ZCAS3-53F4O"
    }
    r=requests.get(url=location_url,params=playload)
    if(r.json()["status"] == 0):
        result = r.json()["result"]
        tmp = {
            "longitude": round(float(result["location"]['lng']) + random.uniform(0.0001, 0.00099), 6),
            "latitude": round(float(result["location"]['lat']) + random.uniform(0.0001, 0.00099), 6),
            "szdq": f"{result['address_components']['province']},{result['address_components']['city']},{result['address_components']['district']}",
            "locationSmall": result["address_components"]["city"] + result["address_components"]["district"] + result[
                "title"],
            "locationBig": f"中国,{result['address_components']['province']},{result['address_components']['city']},{result['address_components']['district']}",
        }
        #更新数据进clockin_data
        config.clockin_data.update(tmp)

    else:
        print("地址查询失败，请参考地图填写")

    return


def send_notification(text):
    if ":" in text:
        type = text.split(":")[0].strip()
        if type not in ["tg","qq"]:
            print("推送不支持 "+type+"，请填写 tg 或 qq")
            return
        key = text.split(":")[1].strip()
    else:
        print("请正确填写推送信息")
        return
    with open("./all.log", 'r',encoding='utf-8') as logfile:
        logs = logfile.read()
    if type == "qq":
        requests.get(url=qq_url+key, params={"msg": logs.strip()}, timeout=1)
    else:
        requests.get(url=tg_url+key+"/")


